package com.addplus.townmall.server.web.provider.serviceimpl.authoritymodule;

import com.addplus.townmall.server.api.constant.ErrorCode;
import com.addplus.townmall.server.api.exception.ErrorException;
import com.addplus.townmall.server.api.mapper.authority.MenuElementMapper;
import com.addplus.townmall.server.api.mapper.authority.UserMapper;
import com.addplus.townmall.server.api.model.authority.MenuElement;
import com.addplus.townmall.server.api.model.authority.User;
import com.addplus.townmall.server.api.service.web.authoritymodule.MenuElementService;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by 特大碗拉面 on 2017/11/9 0009.
 */
@Service(interfaceClass = MenuElementService.class)
public class MenuElementServiceImpl implements MenuElementService {

    @Autowired
    private MenuElementMapper menuElementMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<String> getUserFunctionList(Integer userId) throws Exception {
        if(userId!=null){
           User user= userMapper.selectById(userId);
            if(user!=null){
                List<String> list=  Arrays.asList( user.getRoles().split(","));
                if (!list.isEmpty()) {
                    Integer loginType = Integer.parseInt(RpcContext.getContext().getAttachment("loginType"));
                    List<String> menuElements = menuElementMapper.getUserFunctionList(list, loginType);
                    if(menuElements!=null&&!menuElements.isEmpty()){
                        return menuElements;
                    }else{
                        throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
                    }
                }
            }
            throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);

        }else{
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }

    }

    @Override
    public List<MenuElement> getMenuElementList() throws Exception {
        List<MenuElement> menuElements =menuElementMapper.getMenuElementList();
        if(menuElements!=null&&!menuElements.isEmpty()){
            return menuElements;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
    }

    @Override
    public List<MenuElement> getMenuElementByfunctionId(Integer mId) throws Exception {
        if(mId==null||mId<=0){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        MenuElement menuElement = new MenuElement();
        menuElement.setmId(mId);
        menuElement.setIsDelete(0);
        List<MenuElement> elementList=menuElementMapper.selectList(new QueryWrapper<MenuElement>(menuElement));
        if(elementList!=null&&!elementList.isEmpty()){
            return elementList;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
    }

    @Override
    public MenuElement getMenuElementById(Integer eId) throws Exception {
        if(eId==null||eId<=0){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        MenuElement menuElement = new MenuElement();
        menuElement.setIsDelete(0);
        menuElement.setId(eId);
        MenuElement menuElementNew = menuElementMapper.selectById(menuElement);
        if(menuElementNew!=null){
            return menuElement;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);

    }

    @Override
    public Boolean addMenuElement(MenuElement menuElement) throws Exception {
        if(menuElement==null || menuElement.getmId()==null){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        Date date = new Date();
        menuElement.setIsDelete(0);
        menuElement.setGmtCreate(date);
        menuElement.setGmtModified(date);
        int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
        menuElement.setModifyUser(modifyUser);
        int count =menuElementMapper.insert(menuElement);
        if(count>0) {
            return true;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL,false);
    }

    @Override
    public Boolean deleteMenuElementById(Integer eId) throws Exception{
        if(eId==null||eId<=0){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        MenuElement menuElement = new MenuElement();
        menuElement.setId(eId);
        menuElement.setIsDelete(1);
        int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
        menuElement.setModifyUser(modifyUser);
        int count =menuElementMapper.updateById(menuElement);
        if(count>0){
            return true;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL,false);
    }

    @Override
    public Boolean updateMenuElementById(MenuElement menuElement)throws Exception {
        if(menuElement==null){
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        menuElement.setGmtModified(new Date());
        int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
        menuElement.setModifyUser(modifyUser);
        int count = menuElementMapper.updateById(menuElement);
        if(count>0){
            return true;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL,false);
    }
}
